<?php 
    namespace Mark\Controller;
    
    use Zend\Mvc\Controller\AbstractActionController;
    use Zend\View\Model\ViewModel;
    use Zend\View\Model\JsonModel;
    use Mark\Entity\Mark;
    use Mark\Entity\MarkPhoto;
    use Mark\Entity\MarkVideo;
    use Activity\Entity\MarkActivity;

class MarkController extends AbstractActionController{
        protected $entityComponent;
        protected $markPhotoComponent;
        protected $markVideoComponent;
        protected $photoComponent;
        protected $videoComponent;
        protected $user;
        protected $nbMax = 20;

        protected $activityComponent;
        
        public function init(){
            $this->entityComponent = $this->getServiceLocator()->get('MarkComponent');
            $this->markPhotoComponent = $this->getServiceLocator()->get('MarkPhotoComponent');
            $this->markVideoComponent = $this->getServiceLocator()->get('MarkVideoComponent');
            $this->photoComponent = $this->getServiceLocator()->get('PhotoComponent');
            $this->videoComponent = $this->getServiceLocator()->get('VideoComponent');
            $this->activityComponent = $this->getServiceLocator()->get('ActivityComponent');
            $this->user = $this->identity();
        }

        public function addAction(){
            $this->init();
            if($this->request->isGet()){
            	$message = array();
            	return new ViewModel($message);
            }else{
                $subject = $this->getRequest()->getPost('subject', "");
                $subjectId = $this->getRequest()->getPost('subject_id', 0);
                $value = $this->getRequest()->getPost('mark', null);
                $comment = $this->getRequest()->getPost('comment', "");
                if(in_array($subject, array("photo", "video"))){

                    $mark = new Mark();
                    
                    //try {
                    $mark->mark = $value;
                    $mark->comment = $comment;
                    $mark->user = $this->user;
                    $this->entityComponent->insert($mark);
                    

                    if($mark!=null && $mark->id!=null){
                    	$markActivity = new MarkActivity();
                    	$markActivity->mark = $mark;
                    	$markActivity->date = $mark->createdAt;
                    	$markActivity->user = $mark->user;
                    	$this->activityComponent->insert($markActivity);//Saving the photoActivity
                    }
                    
                    if($subject=="photo"){
                        $photo = $this->photoComponent->get($subjectId);
                        $markPhoto = new MarkPhoto();
                        $markPhoto->mark = $mark;
                        $markPhoto->photo = $photo;
                        $this->markPhotoComponent->insert($markPhoto);
                        $message = array(
                        		'success' => 'Photo Marked. ',
                        		'markPhoto'=>$markPhoto
                        );
                    }else if($subject=="video"){
                        $video = $this->videoComponent->get($subjectId);
                        $markVideo = new MarkVideo();
                        $markVideo->mark = $mark;
                        $markVideo->video = $video;
                        $this->markVideoComponent->insert($markVideo);
                        $message = array(
                        		'success' => 'Video Marked. ',
                        		'markVideo'=>$markVideo
                        );
                    }
                    
                    
                    /*} catch (Exception $e) {
                     $message = array(
                     		'exception' => $e->getTraceAsString()
                     );
                    }*/
                }
                return new JsonModel($message);
            }
        }

        


        public function photoMarksAction()
        {//List marks of a photo given as parameter
            $this->init();
            $id = (int)$this->params()->fromRoute('id', 0);
            $lastId = (int)$this->params()->fromRoute('offset', 0);
            $limit = (int)$this->params()->fromRoute('limit', '5');
            $list = $this->markPhotoComponent->listByPhoto2($id, $lastId, $limit);
            $marks = array();
            foreach ($list as $item){
                $marks[] = $item->getJson();
            }
            $stats = $this->markPhotoComponent->getStats2($id);
            $nb = $stats["nb"];
            $avg = $nb>0?(((int)($stats["total"]*10/$nb))/10):0;
            return new JsonModel(
                array(
                    "marks"=>$marks,
                    "now"=>(new \DateTime("now"))->format("Y-m-d H:i:s"),
                    "count_marks"=>$nb,
                    "avg_marks"=>$avg,
                )
            );
        }
        public function videoMarksAction()
        {//List marks of a video given as parameter
            $this->init();
            $id = (int)$this->params()->fromRoute('id', 0);
            $lastId = (int)$this->params()->fromRoute('offset', 0);
            $limit = (int)$this->params()->fromRoute('limit', '5');
            $list = $this->markVideoComponent->listByVideo2($id, $lastId, $limit);
            $marks = array();
            foreach ($list as $item){
                $marks[] = $item->getJson();
            }
            $stats = $this->markVideoComponent->getStats2($id);
            $nb = $stats["nb"];
            $avg = $nb>0?(((int)($stats["total"]*10/$nb))/10):0;
            return new JsonModel(
                array(
                    "marks"=>$marks,
                    "now"=>(new \DateTime("now"))->format("Y-m-d H:i:s"),
                    "count_marks"=>$nb,
                    "avg_marks"=>$avg,
                )
            );
        }
        
        
        public function markPhotoAction()
        {//Mark a photo and send back the result that can be
            $this->init();
        	$post = $this->request->getPost();
        	$id = (int)$this->params()->fromPost('id');//The id of the photo to mark
        	$user = $this->identity();
        	$value = (int)$this->params()->fromPost('value');
            $comment = $this->params()->fromPost('comment');
            
            $mark = new Mark();
            
            //try {
            $mark->mark = $value;
            $mark->comment = $comment;
            $mark->user = $this->user;
            $this->entityComponent->insert($mark);
            
            
            if($mark!=null && $mark->id!=null){
            	$markActivity = new MarkActivity();
            	$markActivity->mark = $mark;
            	$markActivity->date = $mark->createdAt;
            	$markActivity->user = $mark->user;
            	$this->activityComponent->insert($markActivity);
            	
            	$photo = $this->photoComponent->get($id);
            	$markPhoto = new MarkPhoto();
            	$markPhoto->mark = $mark;
            	$markPhoto->photo = $photo;
            	$this->markPhotoComponent->insert($markPhoto);
            	

            	$stats = $this->markPhotoComponent->getStats2($id);
            	$nb = $stats["nb"];
            	$avg = $nb>0?(((int)($stats["total"]*10/$nb))/10):0;
            	
            	$message = array(
            	    "result_code" => "success",
            		'success' => 'Photo Marked. ',
            	    "mark"=>$markPhoto->getJson(),
                    "now"=>(new \DateTime("now"))->format("Y-m-d H:i:s"),
                    "count_marks"=>$nb,
                    "avg_marks"=>$avg,
            	);
            	return new JsonModel($message);
            	
            	
            }
            return new JsonModel(array("error"=>"mark failed","result_code"=>"error"));
        }
        
        
        public function markVideoAction()
        {//Mark a photo and send back the result that can be
            $this->init();
        	$post = $this->request->getPost();
        	$id = (int)$this->params()->fromPost('id');//The id of the photo to mark
        	$user = $this->identity();
        	$value = (int)$this->params()->fromPost('value');
            $comment = $this->params()->fromPost('comment');
            
            $mark = new Mark();
            
            //try {
            $mark->mark = $value;
            $mark->comment = $comment;
            $mark->user = $this->user;
            $this->entityComponent->insert($mark);
            
            
            if($mark!=null && $mark->id!=null){
            	$markActivity = new MarkActivity();
            	$markActivity->mark = $mark;
            	$markActivity->date = $mark->createdAt;
            	$markActivity->user = $mark->user;
            	$this->activityComponent->insert($markActivity);
            	
            	$video = $this->videoComponent->get($id);
            	$markVideo = new MarkVideo();
            	$markVideo->mark = $mark;
            	$markVideo->video = $video;
            	$this->markVideoComponent->insert($markVideo);
            	

            	$stats = $this->markVideoComponent->getStats2($id);
            	$nb = $stats["nb"];
            	$avg = $nb>0?(((int)($stats["total"]*10/$nb))/10):0;
            	
            	$message = array(
            	    "result_code" => "success",
            		'success' => 'Video Marked. ',
            	    "mark"=>$markVideo->getJson(),
                    "now"=>(new \DateTime("now"))->format("Y-m-d H:i:s"),
                    "count_marks"=>$nb,
                    "avg_marks"=>$avg,
            	);
            	return new JsonModel($message);
            	
            	
            }
            return new JsonModel(array("error"=>"mark failed","result_code"=>"error"));
        }

    }
